<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/1/4
 * Time: 13:00
 */

namespace app\storage\util;

use app\storage\model\StorageModel;
use app\storage\model\UserModel;
use app\storage\service\CommonService;
use think\Session;

trait Common
{
    public function getAssignStorage()
    {
        $storageUuid = input('storageUuid');
        $commonService = new CommonService();
        $storageInfo = $commonService->get_assign_storage($storageUuid);
        $assign['storageUuid'] = $storageUuid;
        $assign['storageInfo'] = $storageInfo;
        return $assign;
    }

    public function getWhere()
    {
        $filter = [];
        $where = [];
        $param = input();
        unset($param['_']);

        if (!empty($param['filter'])) {
            $filter = json_decode($_GET['filter'], true);
            unset($param['filter']);
        }

        foreach ($filter as $k => $value) {
            $value = trim($value);
            $where[$k] = ['like', "%" . $value . "%"];
        }

        $pageSize = $param['pageSize'];
        $pageIndex = $param['pageIndex'];

        $userModel = new UserModel();
        $userId = session('ADMIN_ID');
        $storageList = $userModel->getCurrentStaffStorage();
        if (!empty($param['storageUuid'])) {
            $where['storage_uuid'] = $param['storageUuid'];
        } else if (!empty($storageList) && $userId != 1) {
            array_push($storageList,"");//确保普通用户也能够查出仓库为空的单
            $where['storage_uuid'] = ['in', $storageList];
        }
        else{
            $where['storage_uuid'] =  ['in', StorageModel::getAllStorageUuid()];
        }
        unset($param['pageSize'], $param['pageIndex'],$param['storageUuid']);

        foreach ($param as $key =>$item){
            if(is_array($item)){
                $param[$key] = ['in',$item];
            }
        }

        return [
            'where' => $where,
            'pageIndex' => $pageIndex,
            'pageSize' => $pageSize
        ];
    }


    /**
     * @param string $selectStorage
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function get_assign_storage($selectStorage = '')
    {
        $userId = session('ADMIN_ID');

        if ($userId != 1) {
            $storageModel = new StorageModel();
            $storage = $storageModel->field('uuid,name')->select();
        } else {
            $storageModel = new StorageModel();
            $storage = $storageModel->hasWhere('StaffStorage', ['user_id' => $userId])->select();
        }
        $count = count($storage);
        if (empty($count)) {
            $storage = [];
        } else if (count($storage) == 1) {
            $storage = $storage->toArray();
        } else {
            $storage = $storage->toArray();
            array_push($storage, ['uuid' => '', 'name' => '所有']);
        }

        if (!empty($selectStorage)) {
            $index = array_search($selectStorage, array_column($storage, 'uuid'));
            array_unshift($storage, $storage[$index]);
            unset($storage[$index + 1]);
        }
        return $storage;
    }

    public function getHavingString($having,&$where){
        $temp = [];
        foreach ($having as $item){
            if(!empty($where[$item])){
                $temp[] = "$item like '{$where[$item][1]}'";
                unset($where[$item]);
            }
        }
        $havingString = implode(' and ',$temp);
        return $havingString;
    }

    public function checkExcelUpload(){
        $keyName = 'file_data';
        $error = $_FILES[$keyName]['error'];
        switch (intval($error)) {
            case 0:
                $result = ret_template(true, '配置正确');
                break;
            case 1://文件超过php.ini中设置中upload_max_filesize选项限制的值
                $result = ret_template(false, '上传文件超过后端设置');
                break;
            case 2://文件的大小超过HTML表单中MAX_FILE_SIZE选项的指定的值
                $result = ret_template(false, '上传文件超过前端设置');
                break;
            case 3://文件只有部分被上传
                $result = ret_template(false, '文件部分上传');
                break;
            case 4://没有文件被上传
                $result = ret_template(false, '文件没有被上传');
                break;
            case 5://上传文件大小为0
                $result = ret_template(false, '上传文件大小为0');
                break;
            case 6://找不到临时文件夹
                $result = ret_template(false, '找不到临时文件夹');
                break;
            case 7://文件写入失败，临时文件夹没有写权限或者不存在
                $result = ret_template(false, '临时文件夹没有权限或者不存在');
                break;
            default://错误类型不明确
                $result = ret_template(false, '未知错误');
        }
        return $result;
    }
}